package com.ruoyi.labdata.controller;

import java.util.Date;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.labdata.domain.LabFlowDefination;
import com.ruoyi.labdata.service.ILabFlowDefinationService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 流程定义Controller
 * 
 * @author winner pan
 * @date 2022-03-06
 */
@RestController
@RequestMapping("/labdata/flowDefination")
public class LabFlowDefinationController extends BaseController {
	@Autowired
	private ILabFlowDefinationService labFlowDefinationService;

	/**
	 * 查询流程定义列表
	 */
	@PreAuthorize("@ss.hasPermi('labdata:flowDefination:query')")
	@GetMapping("/list")
	public TableDataInfo list(LabFlowDefination labFlowDefination) {
		startPage();
		List<LabFlowDefination> list = labFlowDefinationService.selectLabFlowDefinationList(labFlowDefination);
		return getDataTable(list);
	}

	@GetMapping("/all")
	public AjaxResult getAll(LabFlowDefination labFlowDefination) {
		List<LabFlowDefination> list = labFlowDefinationService.selectAllFlowDefination(labFlowDefination);
		return AjaxResult.success(list);
	}

	@GetMapping("/preview/{workflowId}")
	public AjaxResult previewDefination(@PathVariable("workflowId") Long workflowId) {
		LabFlowDefination labFlowDefination = labFlowDefinationService.previewFlowDefination(workflowId);
		return AjaxResult.success(labFlowDefination);
	}
	@GetMapping("/business/{businessType}")
	public AjaxResult getFlowDefination(@PathVariable("businessType") String businessType) {
		LabFlowDefination labFlowDefination = labFlowDefinationService.businessFlowDefination(businessType);
		return AjaxResult.success(labFlowDefination);
	}

	/**
	 * 导出流程定义列表
	 */
	// @PreAuthorize("@ss.hasPermi('labdata:flowDefination:export')")
	/*
	 * @Log(title = "流程定义", businessType = BusinessType.EXPORT)
	 * 
	 * @GetMapping("/export") public AjaxResult export(LabFlowDefination
	 * labFlowDefination) { List<LabFlowDefination> list =
	 * labFlowDefinationService.selectLabFlowDefinationList(labFlowDefination);
	 * ExcelUtil<LabFlowDefination> util = new
	 * ExcelUtil<LabFlowDefination>(LabFlowDefination.class); return
	 * util.exportExcel(list, "flowDefination"); }
	 */

	/**
	 * 获取流程定义详细信息
	 */
	@PreAuthorize("@ss.hasPermi('labdata:flowDefination:query')")
	@GetMapping(value = "/{workflowId}")
	public AjaxResult getInfo(@PathVariable("workflowId") Long workflowId) {
		return AjaxResult.success(labFlowDefinationService.selectLabFlowDefinationById(workflowId));
	}

	/**
	 * 新增流程定义
	 */
	@PreAuthorize("@ss.hasPermi('labdata:flowDefination:edit')")
	@Log(title = "流程定义", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody LabFlowDefination labFlowDefination) {
		labFlowDefination.setCreateTime(new Date());
		labFlowDefination.setCreateBy(SecurityUtils.getLoginUser().getUsername());
		return toAjax(labFlowDefinationService.insertLabFlowDefination(labFlowDefination));
	}

	/**
	 * 修改流程定义
	 */
	@PreAuthorize("@ss.hasPermi('labdata:flowDefination:edit')")
	@Log(title = "流程定义", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody LabFlowDefination labFlowDefination) {
		// labFlowDefination.setCreateTime(new Date());
		// labFlowDefination.setCreateBy(SecurityUtils.getLoginUser().getUsername());
		return toAjax(labFlowDefinationService.updateLabFlowDefination(labFlowDefination));
	}

	/**
	 * 删除流程定义
	 */
	@PreAuthorize("@ss.hasPermi('labdata:flowDefination:edit')")
	@Log(title = "流程定义", businessType = BusinessType.DELETE)
	@DeleteMapping("/{workflowId}")
	public AjaxResult remove(@PathVariable Long workflowId) {
		return toAjax(labFlowDefinationService.deleteLabFlowDefinationById(workflowId));
	}
}
